5 ways to drop rows in pandas DataFrame [Practical Examples] | 您所在的位置:网站首页 › dataframe go › 5 ways to drop rows in pandas DataFrame [Practical Examples] |
Table of Contents Advertisement Different methods to drop rows in pandas DataFrameIn this tutorial we will discuss how to drop rows using the following methods: Drop a single Row in DataFrame by Row Index Label Drop a multiple Rows in DataFrame by Row Index Label Drop a single Row in DataFrame by Row Index Position Drop a multiple Rows in DataFrame by Row Index Position Drop Rows in a DataFrame with conditionsCreate pandas DataFrame with example data DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes. We can create the DataFrame by using pandas.DataFrame() method. ALSO READ: Pandas rename column using DataFrame.rename() functionSyntax: pandas.DataFrame(input_data,columns,index)Parameters: It will take mainly three parameters input_data is represents a list of data columns represent the columns names for the data index represent the row numbers/valuesWe can also create a DataFrame using dictionary by skipping columns and indices. Let’s see an example. AdvertisementExample: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names. #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #display the dataframe print(dataframe)Output: id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2Method 1 - Drop a single Row in DataFrame by Row Index Label Here we are going to delete/drop single row from the dataframe using index name/label. Syntax: dataframe.drop('index_label')where, dataframe is the input dataframe index_label represents the index nameExample 1: Drop last row in the pandas.DataFrame In this example we are going to drop last row using row label #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-4 print(dataframe.drop('item-4'))Output: Advertisement id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3ALSO READ: Pandas merge, concat, append, join dataframe - ExamplesExample 2: Drop nth row in the pandas.DataFrame In this example we are going to drop second row using row label #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-2 print(dataframe.drop('item-2'))Output: id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2Method 2 - Drop multiple Rows in DataFrame by Row Index Label Here we are going to delete/drop multiple rows from the dataframe using index name/label. We have to use comma operator to separate the index_labels though a list Syntax: dataframe.drop(['index_label',...............'index_label'])where, dataframe is the input dataframe index_label represents the index name
Example 1: In this example, we are going to drop 2 nd and 4 th row Advertisement #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-2 and item-4 print(dataframe.drop(['item-2','item-4']))Output: id name cost quantity item-1 foo-23 ground-nut oil 567.0 1 item-3 foo-02 flour 67.0 3
Example 2: In this example, we are going to drop 1 st , 2 nd and 4 th row #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-1 , item-2 and item-4 print(dataframe.drop(['item-1','item-2','item-4']))Output: id name cost quantity item-3 foo-02 flour 67.0 3ALSO READ: 5 ways to select multiple columns in a pandas DataFrameMethod 3 - Drop a single Row in DataFrame by Row Index Position Here we are going to delete/drop single row from the dataframe using index position. we have to pass index by using index() method. indexing starts with 0. Syntax: dataframe.drop([dataframe.index[index_position]])where, dataframe is the input dataframe index_position represents the index position
Example 1: In this example we are going to drop last row using row position #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-4 print(dataframe.drop([dataframe.index[3]]))Output: id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3
Example 2- In this example we are going to drop second row using row position #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-2 print(dataframe.drop([dataframe.index[1]]))Output: Advertisement id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2Method 4 - Drop multiple Rows in DataFrame by Row Index Position Here we are going to delete/drop multiple rows from the dataframe using index Position. Syntax: dataframe.drop([dataframe.index[index_position],...............,dataframe.index[index_position]])where, dataframe is the input dataframe index_position represents the index position ALSO READ: How to iterate over rows in Pandas DataFrame [SOLVED]
Example 1: In this example, we are going to drop 2 nd and 4 th row #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-2 and item-4 print(dataframe.drop([dataframe.index[1],dataframe.index[3]]))Output: id name cost quantity item-1 foo-23 ground-nut oil 567.0 1 item-3 foo-02 flour 67.0 3
Example 2: In this example, we are going to drop 1 st , 2 nd and 4 th row #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop item-1, item-2 and item-4 print(dataframe.drop([dataframe.index[0],dataframe.index[1],dataframe.index[3]]))Output: id name cost quantity item-3 foo-02 flour 67.0 3Method 5 - Drop Rows in a DataFrame with conditions Now , we have to drop rows based on the conditions. Just specify the column name with a condition. Syntax: Advertisement dataframe.drop(dataframe[dataframe['column'] operator value].index)where, column refers the column name to be checked with condition operator can be a relational operator index is the function to drop row
Example 1: In this example, we are going to drop the rows based on cost column #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop rows based on cost column with value greater than 300 print(dataframe.drop(dataframe[dataframe['cost'] > 300].index))Output: id name cost quantity item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2ALSO READ: 8 methods to get size of Pandas Series/DataFrame Object Example 2: In this example, we are going to drop the rows based on quantity column #import the module import pandas #consider the food data food_input={'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]} #pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #drop rows nased on quantity column with value less than 3 print(dataframe.drop(dataframe[dataframe['quantity'] < 3].index)) #drop rows based on quantity column with value less than 1 print(dataframe.drop(dataframe[dataframe['quantity'] < 1].index))Output: id name cost quantity item-3 foo-02 flour 67.0 3 id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2Summary We discussed how to drop the row in the Pandas dataframe using four methods with index label and index position. We seen that drop function is the common in all methods and we can also drop/delete the rows conditionally from the dataframe using column. Further Reading Pandas - drop() Categories Python Pandas Didn't find what you were looking for? Perform a quick search across GoLinuxCloud If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form. Thank You for your support!! Before you go.. to stay connected and get the latest updates |
CopyRight 2018-2019 实验室设备网 版权所有 |